package com.ipzoe.app.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ipzoe.app.convert.AgreementConvert;
import com.ipzoe.app.convert.OrderConvert;
import com.ipzoe.app.core.annotation.Login;
import com.ipzoe.app.core.constants.AppConstants;
import com.ipzoe.app.dto.OrderDetailDTO;
import com.ipzoe.cms.domain.Agreement;
import com.ipzoe.cms.domain.Article;
import com.ipzoe.cms.service.IAgreementService;
import com.ipzoe.cms.service.IArticleService;
import com.ipzoe.common.core.controller.BaseController;
import com.ipzoe.common.core.domain.AjaxResult;
import com.ipzoe.mem.domain.Member;
import com.ipzoe.oms.domain.Order;
import com.ipzoe.oms.service.IOrderService;
import com.ipzoe.system.service.ISysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("api/v1/order")
@Api(tags = "订单业务")
@Slf4j
public class OrderController extends BaseController {

    @Autowired
    private IOrderService iOrderService;
    @Autowired
    private IArticleService iArticleService;
    @Autowired
    private IAgreementService iAgreementService;
    @Autowired
    private ISysDictDataService iSysDictDataService;

    /**
     * 查询订单详情
     */
    @Login
    @GetMapping("/info/{id}")
    @ApiOperation("订单详情")
    public AjaxResult detail(@PathVariable Long id, HttpServletRequest request)
    {
        Order order = iOrderService.getById(id);
        OrderDetailDTO orderDetailDTO = OrderConvert.INSTANCE.convert(order);
        if(ObjectUtil.isNull(orderDetailDTO)){
            return AjaxResult.success(new OrderDetailDTO());
        }

        //收费说明
        Article sfsm = iArticleService.getById(3);
        //会员权益
        Article hyqy = iArticleService.getById(4);
        orderDetailDTO.setMemFeeDesc(ObjectUtil.isNotNull(sfsm) ? sfsm.getContent() : "");
        orderDetailDTO.setMemInterestsDesc(ObjectUtil.isNotNull(hyqy) ? hyqy.getContent() : "");

        //支付方式
        if(ObjectUtil.isNotNull(orderDetailDTO.getPayType())){
            orderDetailDTO.setPayTypeStr(iSysDictDataService.selectDictLabel("oms_pay_type",String.valueOf(orderDetailDTO.getPayType())));
        }

        //协议列表
        LambdaQueryWrapper<Agreement> agreementLqw = new QueryWrapper<Agreement>().lambda()
                .eq(Agreement::getPackageId, order.getPackageId()).orderByAsc(Agreement::getId);
        List<Agreement> agreementList = iAgreementService.list(agreementLqw);
        orderDetailDTO.setAgreementList(AgreementConvert.INSTANCE.convert(agreementList));
        return AjaxResult.success(orderDetailDTO);
    }
}
